package cn.wfu.rlrjds.app.repairman.contorller;

import java.io.File;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.wfu.rlrjds.app.log.domain.Log;
import cn.wfu.rlrjds.app.log.service.ILogService;
import cn.wfu.rlrjds.app.repairman.domain.Repaireman;
import cn.wfu.rlrjds.app.repairman.service.IRepairmanService;
import cn.wfu.rlrjds.app.repairman.service.ipml.RepairmanException;
import cn.wfu.rlrjds.app.user.domain.User;
import cn.wfu.rlrjds.app.user.service.ipml.UserEcxeption;
import cn.wfu.rlrjds.container.ServiceProvinder;
import cn.wfu.rlrjds.frame.utils.Const;
import cn.wfu.rlrjds.utils.GsonUtils;
import cn.wfu.rlrjds.utils.ImageUrlUtil;
import cn.wfu.rlrjds.utils.IpUtil;
import cn.wfu.rlrjds.utils.UUIDUtil;

@RequestMapping(value = "/repairman")
@Controller
public class RepairmanContorller {
	@Autowired
	private IRepairmanService repairmanService;
	@Autowired
	private ILogService logService;

	/**
	 * 通过id查找维修员
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/findRepairmanById")
	@ResponseBody
	public String findRepairmanById(HttpServletRequest request) {
		String id = request.getParameter("id");
		try {
			repairmanService.findRepairmanById(id);
			return "false";
		} catch (RepairmanException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "true";
		}
	}

	/**
	 * 查找在线的维修员
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getRepairmansStatus")
	@ResponseBody
	public String getRepairmansStatus(HttpServletRequest request) {
		// 查找在线的维修员
		List<Repaireman> repairmans = this.repairmanService
				.getRepairmansByStatus();
		return GsonUtils.toJson(repairmans);
	}

	/**
	 * 维修员登陆
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/login", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String login(HttpServletRequest request) {
		try {
			String id = request.getParameter("id");
			String ip = IpUtil.getIpAddr(request);
			Repaireman repaireman = repairmanService.login(id);
			Log log = new Log();
			log.setIp(ip);
			log.setLogDate(new Date());
			log.setLogId(UUIDUtil.uuid());
			log.setUserName(repaireman.getId());
			log.setType(1);
			logService.saveLog(log);
			return GsonUtils.toJson(repaireman);
		} catch (RepairmanException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return Const.NAME_NO_EXIST;
		}
	}

	/**
	 * 修改维修员密码
	 * 
	 * @param request
	 * @return
	 * 
	 */
	@RequestMapping(value = "/updatePassword", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String updatePassword(HttpServletRequest request) {
		String id = request.getParameter("id");
		String olderpass = request.getParameter("olderpass");
		String newpass = request.getParameter("newpass");
		Repaireman repaireman = new Repaireman();
		repaireman.setId(id);
		repaireman.setPassword(olderpass);
		try {
			this.repairmanService.updateRepairmanPassword(repaireman, newpass);
			return Const.SUCCESS;
		} catch (RepairmanException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return Const.NOT_FOUND;
		}

	}

	/**
	 * 管理员信息修改
	 * 
	 * @throws Exception
	 * 
	 */
	@RequestMapping(value = "/updateInfo", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String updateInfo(HttpServletRequest request) throws Exception {
		String jsonUserStr = request.getParameter("repaireman");
		String id = request.getParameter("id");
		String ischangeavatar = request.getParameter("ischangeavatar");
		String basepath = request.getServletContext().getRealPath(
				ImageUrlUtil.IMAGE_BASE);
		String saveDir = basepath + "/" + id + "/avatar";
		File baseFile = new File(basepath);
		Repaireman repaireman = GsonUtils.fromJson(jsonUserStr,
				Repaireman.class);
		repaireman.setId(id);
		String phone = repaireman.getPhone();
		if (!phone.matches("^1[3|4|5|7|8]\\d{9}$")) {
			return Const.PHONE_EXIST;
		}
		// 创建文件路径
		File file = new File(saveDir);
		if (!file.exists()) {
			file.mkdirs();
		}
		// 保存文件，设置头像路径
		if (ischangeavatar.equals("true") && repaireman.getAvatarUrl() != null
				&& StringUtils.isNotBlank(repaireman.getAvatarUrl())) {
			String avatarUrl = ImageUrlUtil.getImageURI(
					repaireman.getAvatarUrl(), id, saveDir);
			repaireman.setAvatarUrl(avatarUrl);
		}
		Repaireman updateUser = this.repairmanService.updateRepairemanInfo(
				repaireman, baseFile.getParent());
		String jsonUpdateUserStr = GsonUtils.toJson(updateUser);
		return jsonUpdateUserStr;
	}

	/**
	 * 用户退出
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/logout", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String logout(HttpServletRequest request) {
		String id = request.getParameter("id");
		if (StringUtils.isNotBlank(id)) {
			try {
				this.repairmanService.logout(id);
				return Const.SUCCESS;
			} catch (RepairmanException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return Const.NOT_FOUND;
	}

}